package com.example.My.auToken;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import javax.security.auth.Subject;
import java.util.Collection;

/**
 * Author: 谢宇
 * CreatTime 2023/11/11 17:28
 * Version 1.0.0
 *
 * 由于邮箱验证，类似于UsernamePasswordAuthenticationTokens
 */
public class EmailCodeAuthenticationToken extends AbstractAuthenticationToken {

    private final Object principal;

    //无权限
    public EmailCodeAuthenticationToken(Object principal) {
        super((Collection)null);
        this.principal = principal;
        setAuthenticated(false);
    }

    //开启权限
    public EmailCodeAuthenticationToken(Collection<? extends GrantedAuthority> authorities, Object principal) {
        super(authorities);
        this.principal = principal;
        super.setAuthenticated(true);
    }

    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        } else {
            super.setAuthenticated(false);
        }
    }

    @Override
    public boolean implies(Subject subject) {
        return false;
    }


    @Override
    public Object getCredentials() {
        return null;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }
}
